﻿#include "bookmarkwidget.h"
#include "ui_bookmarkwidget.h"
#include "utils.h"

#include <QFile>

#pragma execution_character_set("utf-8")

QString BookMarkWidget::m_bookMarkFile = "bookmark.txt";

BookMarkWidget::BookMarkWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::BookMarkWidget)
{
    ui->setupUi(this);
    setWindowTitle(tr("我的收藏"));
    // 单击右侧切换选择，双击左侧进入新的页面
    connect(this->ui->listDate, &QListWidget::itemClicked, this, &BookMarkWidget::handleItemClick);

    connect(this->ui->listContent, &QListWidget::itemDoubleClicked, this, &BookMarkWidget::handleItemDoubleClick);
}

BookMarkWidget::~BookMarkWidget()
{
    delete ui;
}


// 每次显示是重新加载历史记录数据
void BookMarkWidget::showEvent(QShowEvent *event)
{
    // 默认显示当天的历史记录
    updateBookMarkData();
}

bool BookMarkWidget::caseInsensitiveLessThan(const HistoryRecord &s1, const HistoryRecord &s2)
  {
    return  s1.m_datetime > s2.m_datetime;
}

bool BookMarkWidget::checkCollected(QString url)
{

    updateBookMarkData();
    bool res = mp.contains(url.trimmed());
    qDebug()<<"check url = "<<url<<", "<<res;
   emit urlCollected(res);
   return res;
}

// 每次更新数据时更新哈希表
void BookMarkWidget::updateBookMarkData(QDateTime startTime)
{
//    qDebug()<<"start time = "<<startTime.toString();
    m_historyRecord.clear();
    mp.clear();
    QVector<HistoryRecord> history = Utils::loadBookMarkRecord(BookMarkWidget::m_bookMarkFile);
    for(int i = 0; i < history.count(); i++){
        if(history[i].dateTime() > startTime){
            m_historyRecord.push_back(history[i]);
            qDebug()<<"insert key = "<<history[i].url().trimmed();
            mp[history[i].url().trimmed()]++;
        }
    }
    // 排序
    qSort(m_historyRecord.begin(), m_historyRecord.end(), BookMarkWidget::caseInsensitiveLessThan);
    ui->listContent->clear();
    for(int i = 0; i < m_historyRecord.count(); i++){
        ui->listContent->addItem(m_historyRecord[i].historyString());
    }
//    qDebug()<<"count = "<<m_historyRecord.count();
}

void BookMarkWidget::reloadHistoryData()
{
    m_historyRecord = Utils::loadHistoryRecord(BookMarkWidget::m_bookMarkFile);
}



void BookMarkWidget::handleItemClick(QListWidgetItem *item)
{
    int idx = ui->listDate->row(item);
    if(idx == 0){
        // 显示今天的历史记录
//        QDateTime time = QDateTime::currentDateTime().addSecs(-60 * 60 * 1);   // 过去一小时
        QDateTime time = QDateTime::currentDateTime().addDays(-1);
        updateBookMarkData(time);
    }else if(idx == 1){
        // 显示过去7天的历史记录
        QDateTime time = QDateTime::currentDateTime().addDays(-7);
        updateBookMarkData(time);
    }else if(idx == 2){
        // 显示过去一个月的历史记录
        QDateTime time = QDateTime::currentDateTime().addMonths(-1);
        updateBookMarkData(time);
    }else{
        // 显示所有的历史记录
        updateBookMarkData(QDateTime::currentDateTime().addYears(-10));
    }
}

void BookMarkWidget::handleItemDoubleClick(QListWidgetItem *item)
{
    QString url = item->text().split("#")[1];
    emit loadUrl(url);
}

void BookMarkWidget::addBookMark(QString url)
{
    qDebug()<<"addBookMark, "<<url;
    if(checkCollected(url)){
        qDebug()<<"url existed";
        return;
    }else {
        Utils::writeBookMark(url);
        updateBookMarkData();
    }
}
